﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Common/ResultSummaryHelper
* 类       名: ResultSummaryHelper
* 功      能:  TD
* 时      间:  2023/9/2 14:16:04
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using HslCommunication.Core.Net;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime.Workdays;
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.DAL.DataEntity;
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;

namespace SiliconSelect.Common
{
    public class ResultSummaryHelper
    {
        /// <summary>
        /// 分选的时候检测是否所有的工位都接收到了消息.
        /// </summary>
        internal static DataResult CheckMsgIsComplete(string waferId)
        {
            string msgLoss = "";
            DataResult result = new DataResult();
            try
            {
                if (GlobalValues.WaferIdFromPcDictionary.ContainsKey(waferId))
                {
                    List<string> pcNameList = GlobalValues.WaferIdFromPcDictionary[waferId];
                    if (!pcNameList.Contains("FrontEdge") && !GlobalValues.ModuleIsShield.Contains("前崩"))
                    {
                        msgLoss += "前崩 ";
                    }
                    if (!pcNameList.Contains("BackEdge") && !GlobalValues.ModuleIsShield.Contains("后崩"))
                    {
                        msgLoss += "后崩 ";
                    }
                    if (!pcNameList.Contains("LeftEdge") && !GlobalValues.ModuleIsShield.Contains("左崩"))
                    {
                        msgLoss += "左崩 ";
                    }
                    if (!pcNameList.Contains("RightEdge") && !GlobalValues.ModuleIsShield.Contains("右崩"))
                    {
                        msgLoss += "右崩 ";
                    }
                    if (!pcNameList.Contains("SmuUp") && !GlobalValues.ModuleIsShield.Contains("上脏污"))
                    {
                        msgLoss += "上脏污 ";
                    }
                    if (!pcNameList.Contains("SmuDown") && !GlobalValues.ModuleIsShield.Contains("下脏污"))
                    {
                        msgLoss += "下脏污 ";
                    }
                    if (!pcNameList.Contains("ThreeD") && !GlobalValues.ModuleIsShield.Contains("3D"))
                    {
                        msgLoss += "3D ";
                    }
                    if (!pcNameList.Contains("Size") && !GlobalValues.ModuleIsShield.Contains("尺寸"))
                    {
                        msgLoss += "尺寸 ";
                    }
                    if (!pcNameList.Contains("Subfi") && !GlobalValues.ModuleIsShield.Contains("隐裂"))
                    {
                        msgLoss += "隐裂 ";
                    }
                    if (!pcNameList.Contains("电阻率") && !GlobalValues.ModuleIsShield.Contains("电阻率"))
                    {
                        msgLoss += "电阻率 ";
                    }
                    if (msgLoss == "")
                    {
                        result.State = true;
                        return result;
                    }
                    if (pcNameList.Count == 1 && pcNameList[0].Equals("电阻率"))
                    {
                        msgLoss = "电阻率误判";
                    }
                }
                else
                {
                    msgLoss += "流水号消息缺失, 所有工位";
                }
                result.Message = msgLoss;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogType.Exception, ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!));
                result.Message = ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!);
                return result;
            }
            return result;
        }

        /// <summary>
        /// 从数据库中查询要汇总的数据,这样不用管顺序了,可以自定义插入数据的顺序.
        /// </summary>
        /// <param name="waferId"></param>
        /// <returns></returns>
        internal static List<string> GetSummaryResListFromGlobal(string waferId)
        {

            DataResult<List<string>> result = new DataResult<List<string>>();
            List<string> waferDataList = new List<string>();
            // 头数据
            if (GlobalValues.WaferDataComman.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataComman[waferId]).ToList();
                GlobalValues.WaferDataComman.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.CommanTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 3D第一部分数据
            if (GlobalValues.WaferData3d01.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferData3d01[waferId]).ToList();
                GlobalValues.WaferData3d01.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.Title3D01.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 电阻率数据
            if (GlobalValues.WaferDataResistivity.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataResistivity[waferId]).ToList();
                GlobalValues.WaferDataResistivity.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.ResistivityTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 尺寸数据
            if (GlobalValues.WaferDataSize.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataSize[waferId]).ToList();
                GlobalValues.WaferDataSize.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.SizeTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 崩边数据
            if (GlobalValues.WaferDataEdgeBreak.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataEdgeBreak[waferId]).ToList();
                GlobalValues.WaferDataEdgeBreak.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.EdgeBreakFrontTitle.Length + GlobalValues.EdgeBreakBackTitle.Length + GlobalValues.EdgeBreakLeftTitle.Length + GlobalValues.EdgeBreakRightTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 3D 的另外一组数据
            if (GlobalValues.WaferData3D02.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferData3D02[waferId]).ToList();
                GlobalValues.WaferData3D02.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.Title3D02.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 脏污工位数据
            if (GlobalValues.WaferDataSumdge.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataSumdge[waferId]).ToList();
                GlobalValues.WaferDataSumdge.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.SumdgeUpTitle.Length + GlobalValues.SumdgeDownTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }

            // 隐裂工位数
            if (GlobalValues.WaferDataSubfissure.ContainsKey(waferId))
            {
                waferDataList = waferDataList.Concat(GlobalValues.WaferDataSubfissure[waferId]).ToList();
                GlobalValues.WaferDataSubfissure.Remove(waferId);
            }
            else
            {
                string[] temp = new string[GlobalValues.SubfissureTitle.Length];
                Array.Fill(temp, string.Empty);
                waferDataList = waferDataList.Concat(temp).ToList();
            }
            return waferDataList;
        }

        /// <summary>
        /// 根据流水号去获取一条完整的数据,这一步还是很重要,因为什么重要,因为从数据库获取很耗时,应该用一个临时变量去保存比较好.
        /// </summary>
        /// <param name="waferIdFromPlc"></param>
        /// <returns></returns>
        //internal static List<string> GetSummaryResList(string waferId)
        //{
        //    string[] res = new string[GlobalValues.CommanTitle.Length];
        //    try
        //    {
        //        if(GlobalValues.CurrentDetectedSumRes.ContainsKey(waferId))
        //        {
        //            res = GlobalValues.CurrentDetectedSumRes[waferId];
        //            GlobalValues.CurrentDetectedSumRes.Remove(waferId); // 移除掉
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.WriteLog(LogType.Exception, ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!));
        //    }
        //    return res.ToList();
        //}
    }
}
